/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月27日         Root.Lu
 */

package com.coocaa.launcher.statusplugins.calendar;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.coocaa.launcher.framework.launcherhost.statusbar.StatusBarPlugin;
import com.coocaa.launcher.framework.launcherhost.statusbar.StatusBarPluginView;

public class StatusPluginCalendar extends StatusBarPlugin {
    public interface IStatusPluginCalendarView {
        void updateString(String string, boolean isVisible);
    }

    public static StatusPluginCalendar instance = new StatusPluginCalendar();

    // private static final String DEFAULT_DISPLAY_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String DEFAULT_DISPLAY_FORMAT = "HH:mm  yyyy/MM/dd";

    private StatusBarPluginView view = null;
    private IStatusPluginCalendarView iview = null;

    private StatusPluginCalendar() {
        super("statusplugin.calendar");
        // TODO Auto-generated constructor stub
    }

    private Runnable runnable = new Runnable() {
        @SuppressLint("SimpleDateFormat")
        @Override
        public void run() {
            // TODO Auto-generated method stub
            Date date = new Date(System.currentTimeMillis());
            SimpleDateFormat f = new SimpleDateFormat(DEFAULT_DISPLAY_FORMAT);

            ConnectivityManager connectivityManager = (ConnectivityManager) mContext
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo info = connectivityManager.getActiveNetworkInfo();
            if (info != null && info.isAvailable() && info.isConnected()) {
                if (iview != null)
                    iview.updateString(f.format(date), true);
            } else {
                if (iview != null)
                    iview.updateString(f.format(date), false);
            }
            postAction(runnable, 1000);
        }
    };

    @Override
    protected void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        postAction(runnable, 1000);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        view = null;
        iview = null;
    }

    @Override
    public synchronized StatusBarPluginView inflate(String pattern) {
        // TODO Auto-generated method stub
        if (view == null) {
            view = new StatusPluginCalendarView(mContext, getName());
            iview = (IStatusPluginCalendarView) view;
        }
        return view;
    }

}
